package com.aplink.generic.widgets;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.type.ControlStatus;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.controlspec.ButtonSpec;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.util.Language;

public class ApLinkButton extends ApLinkBaseView {

	private final Button mButton;
	private final ButtonSpec mButtonSpec;
	private String mOldTranslation;

	public ApLinkButton(final GenericFragment dataEntryActivity,
	        final ControlSpec controlSpec, final View view, Bundle bundle) {
		super(dataEntryActivity, controlSpec, view, bundle);
		this.mButton = (Button) view;
		this.mButtonSpec = (ButtonSpec) controlSpec;
	}

	@Override
	public void clear() {

	}

	@Override
	public GenericAttribute getAttribute() {
		return null;
	}

	@Override
	public GenericDictionary<String, GenericAttribute> getListAttributes() {
		return null;
	}

	@Override
	public Button getView() {
		return this.mButton;
	}

	@Override
	protected void initViewProperties() {
		this.mButton.setTag(this.mButtonSpec);
		mOldTranslation = mButton.getText().toString();
		mButton.setText(Language.getInstance()
		        .get(mButton.getText().toString()));

		ControlStatus controlStatus = mButtonSpec.getControlStatus();
		if (controlStatus.getValue() >= 100
		        && !controlStatus.equals(ControlStatus.None)) {
			mButton.setEnabled(controlStatus.equals(ControlStatus.Enable));
		} else if (!controlStatus.equals(ControlStatus.None)) {
			mButton.setVisibility(controlStatus.getValue());
		}
	}

	@Override
	public void languageChanged() {
		mButton.setText(Language.getInstance().get(mOldTranslation));
	}

	@Override
	protected void loadBindingObject() {
		if (getPassedContent() != null) {
			this.mButton.setText(getPassedContent());
		}
	}

	@Override
	protected void loadDefaultObject() {

	}

	@Override
	protected void setEventListener() {
		if (this.mButtonSpec.getOnClickListener() != null) {
			this.mButton.setOnClickListener(this.mButtonSpec
			        .getOnClickListener());
		} else {
			this.mButton.setOnClickListener(getGenericFragment());
		}
	}

	@Override
	public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
		throw new UnsupportedOperationException(this.getClass().getSimpleName()
		        + " is not support this method");
	}

	@Override
	public void setOnClickListener(final OnClickListener onClickListener) {
		this.mButton.setOnClickListener(onClickListener);
	}
}
